package com.lfy.modules.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfy.modules.blog.domain.RoleEntity;
import com.lfy.modules.blog.domain.vo.role.RoleVO;
import com.lfy.modules.blog.mapper.RoleEntityMapper;
import com.lfy.modules.blog.service.RoleService;
import com.lfy.modules.common.domain.PageResultVO;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.List;
import java.util.stream.Collectors;

/**
* @author 吕飞扬
* @description 针对表【role】的数据库操作Service实现
* @createDate 2024-04-03 01:14:45
*/
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleEntityMapper, RoleEntity>
    implements RoleService {

    /**
     * 获取role列表
     * @param pageSize 每页大小
     * @param pageNum 当前页码
     * @return r
     */
    @Override
    public R<PageResultVO<RoleVO>> getRoleList(Integer pageSize, Integer pageNum,String name) {
        LambdaQueryWrapper<RoleEntity> roleEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)){
            roleEntityLambdaQueryWrapper.like(RoleEntity::getRoleName,name);
        }
        Page<RoleEntity> roleEntityPage = new Page<>(pageNum, pageSize);
        IPage<RoleEntity> roleEntityIPage = this.baseMapper.selectPage(roleEntityPage, roleEntityLambdaQueryWrapper);
        List<RoleEntity> records = roleEntityIPage.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return R.success(PageUtil.convert2PageEmptyResult(roleEntityIPage, RoleVO.class));
        }
        List<RoleVO> voResult = records.stream().map(item -> {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(item, roleVO);
            return roleVO;
        }).collect(Collectors.toList());

        return R.success(PageUtil.buildPageResultVO(roleEntityIPage, voResult));
    }
}




